package com.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 * Created by marlon on 2016-05-04.
 */
public class MyViewPager extends ViewPager {
    private float touchX = 0;
    private float touchY = 0;
    private float moveX = 0;
    private float moveY = 0;
    private Context context;
    public MyViewPager(Context context) {
        super(context);
        this.context=context;
    }

    public MyViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            touchX = ev.getX();
            touchY = ev.getY();
        }
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            moveX = ev.getX();
            moveY = ev.getY();
            //若水平滑动距离大则拦截事件，且滑动距离超过最小滑动距离，这样ViewPager就可以水平滑动
            if (Math.abs(moveY - touchY) < Math.abs(moveX - touchX)&&Math.abs(moveX - touchX)> ViewConfiguration.get(context).getScaledTouchSlop()) {
                return true;
            }
            return false;
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            return false;
        }
        return super.onInterceptTouchEvent(ev);
    }
}
